<?php

namespace Cck\ArticlesSystem\Services;

use Cck\ArticlesSystem\Http\Models\Setting;

class SettingsService
{
    protected $settings = null;

    public function __construct()
    {
        $this->settings = Setting::query()
            ->with('group')
            ->get();
        // ->keyBy(function (Setting $setting) {
        //     return $setting->group->name . '.' . $setting->name;
        // });
    }

    public function getSetting(string $name): ?Setting
    {
        return $this->settings->get($name) ?? null;
    }

    public function getSettingValue(string $name, $default = null)
    {
        $setting = $this->getSetting($name);

        return is_null($setting) ? $default : $setting->casted_value;
    }
}
